<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Pelajaran_model extends CI_Model {

	function __construct()
	{
		parent::__construct();
		$this->CI = get_instance();
	}
	
	//==================================================================
	function get_pelajaran(){
		
		$this->db->select('p.*, p.KETERANGAN as KET, p.ID_SEKOLAH as ID_SEK, s.NAMA_SEKOLAH, a.TAHUN_AJARAN as NAMA_TA, p.TAHUN_AJARAN as ID_TA');
		$this->db->from('tb_pelajaran p'); 
		$this->db->join('tb_master_sekolah s','s.ID_SEKOLAH=p.ID_SEKOLAH');
		$this->db->join('tb_master_tahun_ajaran a','a.ID_TA=p.TAHUN_AJARAN');
		$this->db->where('a.ISAKTIF',1);
		$this->CI->flexigrid->build_query();
		
		$return['records'] = $this->db->get();
		
		$this->db->select('p.*, p.KETERANGAN as KET, p.ID_SEKOLAH as ID_SEK, s.NAMA_SEKOLAH, p.TAHUN_AJARAN as TA, p.TAHUN_AJARAN as ID_TA');
		$this->db->from('tb_pelajaran p'); 
		$this->db->join('tb_master_sekolah s','s.ID_SEKOLAH=p.ID_SEKOLAH');
		$this->db->join('tb_master_tahun_ajaran a','a.ID_TA=p.TAHUN_AJARAN');
		$this->db->where('a.ISAKTIF',1);
		$this->CI->flexigrid->build_query(FALSE);
		
		$return['record_count'] = $this->db->count_all_results();

		return $return;
	}
	
	function get_pelajaranID($id_sekolah, $thn_ajaran, $kode_mp){
		
		//Select table column
		$this->db->select('p.*, p.KETERANGAN as KET, p.ID_SEKOLAH as ID_SEK, s.NAMA_SEKOLAH, a.TAHUN_AJARAN as NAMA_TA, p.TAHUN_AJARAN as ID_TA');
		$this->db->from('tb_pelajaran p');
		$this->db->join('tb_master_sekolah s','s.ID_SEKOLAH=p.ID_SEKOLAH');
		$this->db->join('tb_master_tahun_ajaran a','a.ID_TA=p.TAHUN_AJARAN');
		$this->db->where('p.ID_SEKOLAH',$id_sekolah);
		$this->db->where('p.TAHUN_AJARAN',$thn_ajaran);
		$this->db->where('p.KODE_MATA_PELAJARAN',$kode_mp);
		$query = $this->db->get();
		return $query;
	}
	function insert_pelajaran($data){
		$this->db->insert('tb_pelajaran',$data);
	}
	
	function update_pelajaran($data,$id_sekolah, $thn_ajaran, $kode_mp){
		$this->db->where('ID_SEKOLAH',$id_sekolah);
		$this->db->where('TAHUN_AJARAN',$thn_ajaran);
		$this->db->where('KODE_MATA_PELAJARAN',$kode_mp);
		$this->db->update('tb_pelajaran',$data);
	}
	
	function delete_pelajaran($param){
		$param_exp = explode('#',$param);
		$id_sekolah = $param_exp[0];
		$thn_ajaran = $param_exp[1];
		$kode_mp = $param_exp[2];
		
		$this->db->where('ID_SEKOLAH',$id_sekolah);
		$this->db->where('TAHUN_AJARAN',$thn_ajaran);
		$this->db->where('KODE_MATA_PELAJARAN',$kode_mp);
		$this->db->delete('tb_pelajaran'); 
	}
	
	function validasi_pelajaran($kode_mp,$thn_ajaran,$id_sekolah){
		
		//Select table column
		$this->db->select('count(*) as jml, KODE_MATA_PELAJARAN');
		$this->db->from('tb_pelajaran');
		$this->db->where('TAHUN_AJARAN',$thn_ajaran);
		$this->db->where('ID_SEKOLAH',$id_sekolah);
		$this->db->where('KODE_MATA_PELAJARAN',$kode_mp);
		$query = $this->db->get();
		return $query;
	}
	
	function getLastMp(){
		$this->db->select('max(KODE_MP) as maxid');
		$this->db->from('tb_pelajaran');
		$query = $this->db->get();
		return $query;
	}
	
	function get_mp(){
		//Select table column
		$this->db->select('*');
		$this->db->from('tb_pelajaran');
		$this->db->where('TAHUN_AJARAN',$this->session->userdata('tahunajaran'));
		$query = $this->db->get();
		return $query;
	}
	
	//==============================================
	function get_aspeknilai($kode_mp,$id_sekolah,$id_ta){
		
		$this->db->select('pel.NAMA_MATA_PELAJARAN, p.*,p.KETERANGAN as KET, p.ID_SEKOLAH as ID_SEK, s.NAMA_SEKOLAH, a.TAHUN_AJARAN as NAMA_TA, p.TAHUN_AJARAN as ID_TA');
		$this->db->from('tb_pelajaran_aspek_nilai p'); 
		$this->db->join('tb_master_sekolah s','s.ID_SEKOLAH=p.ID_SEKOLAH');
		$this->db->join('tb_master_tahun_ajaran a','a.ID_TA=p.TAHUN_AJARAN');
		$this->db->join('tb_pelajaran pel','pel.KODE_MATA_PELAJARAN=p.KODE_MATA_PELAJARAN and pel.TAHUN_AJARAN=p.TAHUN_AJARAN');
		$this->db->where('p.ID_SEKOLAH',$id_sekolah);
		$this->db->where('p.TAHUN_AJARAN',$id_ta);
		$this->db->where('p.KODE_MATA_PELAJARAN',$kode_mp);
		$this->CI->flexigrid->build_query();
		
		$return['records'] = $this->db->get();
		
		$this->db->select('pel.NAMA_MATA_PELAJARAN, p.*,p.KETERANGAN as KET, p.ID_SEKOLAH as ID_SEK, s.NAMA_SEKOLAH, a.TAHUN_AJARAN as NAMA_TA, p.TAHUN_AJARAN as ID_TA');
		$this->db->from('tb_pelajaran_aspek_nilai p'); 
		$this->db->join('tb_master_sekolah s','s.ID_SEKOLAH=p.ID_SEKOLAH');
		$this->db->join('tb_master_tahun_ajaran a','a.ID_TA=p.TAHUN_AJARAN');
		$this->db->join('tb_pelajaran pel','pel.KODE_MATA_PELAJARAN=p.KODE_MATA_PELAJARAN and pel.TAHUN_AJARAN=p.TAHUN_AJARAN');
		$this->db->where('p.ID_SEKOLAH',$id_sekolah);
		$this->db->where('p.TAHUN_AJARAN',$id_ta);
		$this->db->where('p.KODE_MATA_PELAJARAN',$kode_mp);
		$this->CI->flexigrid->build_query(FALSE);
		
		$return['record_count'] = $this->db->count_all_results();

		return $return;
	}
	function get_aspeknilaiID($id_aspek){
		
		//Select table column
		$this->db->select('p.*, p.KETERANGAN as KET, p.ID_SEKOLAH as ID_SEK, s.NAMA_SEKOLAH, a.TAHUN_AJARAN as NAMA_TA, p.TAHUN_AJARAN as ID_TA');
		$this->db->from('tb_pelajaran_aspek_nilai p');
		$this->db->join('tb_master_sekolah s','s.ID_SEKOLAH=p.ID_SEKOLAH');
		$this->db->join('tb_master_tahun_ajaran a','a.ID_TA=p.TAHUN_AJARAN');
		$this->db->where('p.ID_ASPEK',$id_aspek);
		// $this->db->where('p.TAHUN_AJARAN',$id_ta);
		// $this->db->where('p.KODE_MATA_PELAJARAN',$kode_mp);
		// $this->db->where('p.KODE_PENILAIAN',$kode_nilai);
		$query = $this->db->get();
		return $query;
	}
	function insert_aspeknilai($data){
		$this->db->insert('tb_pelajaran_aspek_nilai',$data);
	}
	function update_aspeknilai($data, $id_aspek){
		$this->db->where('ID_ASPEK',$id_aspek);
		$this->db->update('tb_pelajaran_aspek_nilai',$data);
	}
	
	function delete_aspeknilai($id){
		// $param_exp = explode('#',$param);
		
		$this->db->where('ID_ASPEK',$id);
		$this->db->delete('tb_pelajaran_aspek_nilai'); 
	}
	function validasi_aspeknilai($kode_mp, $id_sekolah, $id_ta, $kode_nilai){
		
		//Select table column
		$this->db->select('count(*) as jml, ID_ASPEK');
		$this->db->from('tb_pelajaran_aspek_nilai');
		$this->db->where('TAHUN_AJARAN',$id_ta);
		$this->db->where('ID_SEKOLAH',$id_sekolah);
		$this->db->where('KODE_MATA_PELAJARAN',$kode_mp);
		$this->db->where('KODE_PENILAIAN',$kode_nilai);
		// if($id_aspek != null)
			// $this->db->where('ID_ASPEK !=',$id_aspek);
		$query = $this->db->get();
		return $query;
	}
	
	function combo_mp($id_ta){
		$this->db->select('*');
		$this->db->from('tb_pelajaran');
		$this->db->where('TAHUN_AJARAN',$id_ta);
		$query = $this->db->get();
		return $query;
	}
	
	
	function getLastAspek(){
		$this->db->select('max(ID_ASPEK) as maxid');
		$this->db->from('tb_pelajaran_aspek_nilai');
		$query = $this->db->get();
		return $query;
	}
	//======================================================
	function get_pelajaran_guru($kode_mp,$id_sekolah,$id_ta,$id_tingkat='',$id_kelas='',$id_smt=''){
		
		$this->db->select('peg.NAMA_LENGKAP,peg.GELAR_DEPAN,peg.GELAR_BELAKANG, mp.NAMA_MATA_PELAJARAN, t.TINGKAT,t.ID_TINGKAT,k.KELAS,k.ID_KELAS,smt.SEMESTER,smt.ID_SMT, p.*, p.KETERANGAN as KET, p.ID_SEKOLAH as ID_SEK, s.NAMA_SEKOLAH, a.TAHUN_AJARAN as NAMA_TA, p.TAHUN_AJARAN as ID_TA');
		$this->db->from('tb_pelajaran_guru p'); 
		$this->db->join('tb_master_sekolah s','s.ID_SEKOLAH=p.ID_SEKOLAH');
		$this->db->join('tb_master_tahun_ajaran a','a.ID_TA=p.TAHUN_AJARAN');
		$this->db->join('tb_master_tingkat t','t.ID_TINGKAT=p.ID_TINGKAT');
		$this->db->join('tb_master_kelas k','k.ID_KELAS=p.ID_KELAS');
		$this->db->join('tb_master_semester smt','smt.ID_SMT=p.ID_SEMESTER');
		$this->db->join('tb_pelajaran mp','mp.KODE_MATA_PELAJARAN=p.KODE_MATA_PELAJARAN and mp.TAHUN_AJARAN=p.TAHUN_AJARAN');
		$this->db->join('tb_pegawai peg','peg.NIP=p.NIP');
		$this->db->where('p.TAHUN_AJARAN',$id_ta);
		$this->db->where('p.ID_SEKOLAH',$id_sekolah);
		$this->db->where('p.KODE_MATA_PELAJARAN',$kode_mp);
		// if($id_tingkat!='' and $id_kelas!='' and $id_smt!=''){
			$this->db->where('p.ID_TINGKAT',$id_tingkat);
			$this->db->where('p.ID_KELAS',$id_kelas);
			$this->db->where('p.ID_SEMESTER',$id_smt);
		// }
		$this->CI->flexigrid->build_query();
		
		$return['records'] = $this->db->get();
		
		$this->db->select('peg.NAMA_LENGKAP,peg.GELAR_DEPAN,peg.GELAR_BELAKANG, mp.NAMA_MATA_PELAJARAN, t.TINGKAT,t.ID_TINGKAT,k.KELAS,k.ID_KELAS,smt.SEMESTER,smt.ID_SMT, p.*, p.KETERANGAN as KET, p.ID_SEKOLAH as ID_SEK, s.NAMA_SEKOLAH, a.TAHUN_AJARAN as NAMA_TA, p.TAHUN_AJARAN as ID_TA');
		$this->db->from('tb_pelajaran_guru p'); 
		$this->db->join('tb_master_sekolah s','s.ID_SEKOLAH=p.ID_SEKOLAH');
		$this->db->join('tb_master_tahun_ajaran a','a.ID_TA=p.TAHUN_AJARAN');
		$this->db->join('tb_master_tingkat t','t.ID_TINGKAT=p.ID_TINGKAT');
		$this->db->join('tb_master_kelas k','k.ID_KELAS=p.ID_KELAS');
		$this->db->join('tb_master_semester smt','smt.ID_SMT=p.ID_SEMESTER');
		$this->db->join('tb_pelajaran mp','mp.KODE_MATA_PELAJARAN=p.KODE_MATA_PELAJARAN and mp.TAHUN_AJARAN=p.TAHUN_AJARAN');
		$this->db->join('tb_pegawai peg','peg.NIP=p.NIP');
		$this->db->where('p.TAHUN_AJARAN',$id_ta);
		$this->db->where('p.ID_SEKOLAH',$id_sekolah);
		$this->db->where('p.KODE_MATA_PELAJARAN',$kode_mp);
		// if($id_tingkat!='' and $id_kelas!='' and $id_smt!=''){
			$this->db->where('p.ID_TINGKAT',$id_tingkat);
			$this->db->where('p.ID_KELAS',$id_kelas);
			$this->db->where('p.ID_SEMESTER',$id_smt);
		// }
		$this->CI->flexigrid->build_query(FALSE);
		
		$return['record_count'] = $this->db->count_all_results();

		return $return;
	}
	
	function get_pelajaranguruID($id_pg){
		
		//Select table column
		$this->db->select('p.*, p.KETERANGAN as KET, p.ID_SEKOLAH as ID_SEK, s.NAMA_SEKOLAH, a.TAHUN_AJARAN as NAMA_TA, p.TAHUN_AJARAN as ID_TA,p.NIP as NIP_GURU');
		$this->db->from('tb_pelajaran_guru p');
		$this->db->join('tb_master_sekolah s','s.ID_SEKOLAH=p.ID_SEKOLAH');
		$this->db->join('tb_master_tahun_ajaran a','a.ID_TA=p.TAHUN_AJARAN');
		$this->db->where('p.ID_PG',$id_pg);
		$query = $this->db->get();
		return $query;
	}
	
	function insert_pelajaran_guru($data){
		$this->db->insert('tb_pelajaran_guru',$data);
	}
	function update_pelajaran_guru($data, $id_pg){
		$this->db->where('ID_PG',$id_pg);
		$this->db->update('tb_pelajaran_guru',$data);
	}
	
	function delete_pelajaran_guru($id_pg){
		// $param_exp = explode('#',$param);
		
		// $this->db->where('ID_SEKOLAH',$id_sekolah);
		// $this->db->where('TAHUN_AJARAN',$id_ta);
		// $this->db->where('KODE_MATA_PELAJARAN',$kode_mp);
		// $this->db->where('KODE_PENILAIAN',$kode_nilai);
		$this->db->where('ID_PG',$id_pg);
		$this->db->delete('tb_pelajaran_guru'); 
	}
	function validasi_pelajaran_guru($kode_mp, $id_sekolah, $thn_ajaran, $tingkat, $kelas, $smt, $nip){
		
		//Select table column
		$this->db->select('count(*) as jml, ID_PG');
		$this->db->from('tb_pelajaran_guru');
		$this->db->where('TAHUN_AJARAN',$thn_ajaran);
		$this->db->where('ID_SEKOLAH',$id_sekolah);
		$this->db->where('KODE_MATA_PELAJARAN',$kode_mp);
		$this->db->where('ID_TINGKAT',$tingkat);
		$this->db->where('ID_KELAS',$kelas);
		$this->db->where('ID_SEMESTER',$smt);
		$this->db->where('NIP',$nip);
		$query = $this->db->get();
		return $query;
	}
	
	function getTa($id_sekolah){
		$this->db->select('*');
		$this->db->from('tb_master_sekolah');
		$this->db->where('ID_SEKOLAH',$id_sekolah);
		$query_dep = $this->db->get()->row()->ID_DEPARTMENT;
		
		$this->db->select('TAHUN_AJARAN,ID_TA');
		$this->db->from('tb_master_tahun_ajaran');
		$this->db->where('ID_SEKOLAH',$id_sekolah);
		$this->db->where('DEPARTMENT',$query_dep);
		$this->db->where('ISAKTIF',1);
		$query = $this->db->get();
		return $query;
	}
	function getMp($id_sekolah,$kodemp){
		$this->db->select('*');
		$this->db->from('tb_master_sekolah');
		$this->db->where('ID_SEKOLAH',$id_sekolah);
		$query_dep = $this->db->get()->row()->ID_DEPARTMENT;
		
		$this->db->select('ID_TA');
		$this->db->from('tb_master_tahun_ajaran');
		$this->db->where('ID_SEKOLAH',$id_sekolah);
		$this->db->where('DEPARTMENT',$query_dep);
		$this->db->where('ISAKTIF',1);
		$query_ta = $this->db->get()->row()->ID_TA;
		
		$this->db->select('NAMA_MATA_PELAJARAN');
		$this->db->from('tb_pelajaran');
		$this->db->where('KODE_MATA_PELAJARAN',$kodemp);
		$this->db->where('ID_SEKOLAH',$id_sekolah);
		$this->db->where('TAHUN_AJARAN',$query_ta);
		$query = $this->db->get();
		return $query;
	}
	
	function getTingkat($id_sekolah,$id_tingkat=''){
		$this->db->select('*');
		$this->db->from('tb_master_sekolah');
		$this->db->where('ID_SEKOLAH',$id_sekolah);
		$query_dept = $this->db->get()->row()->ID_DEPARTMENT;
		
		$this->db->select('*');
		$this->db->from('tb_master_tingkat');
		$this->db->where('ID_SEKOLAH',$id_sekolah);
		$this->db->where('DEPARTMENT',$query_dept);
		$this->db->where('ISAKTIF',1);
		if($id_tingkat!='')
			$this->db->where('ID_TINGKAT',$id_tingkat);
		$query = $this->db->get();
		return $query;
	}
	function getKelas($id_sekolah,$id_kelas=''){
		$this->db->select('*');
		$this->db->from('tb_master_sekolah');
		$this->db->where('ID_SEKOLAH',$id_sekolah);
		$query_dep = $this->db->get()->row()->ID_DEPARTMENT;
	
		$this->db->select('ID_TA');
		$this->db->from('tb_master_tahun_ajaran');
		$this->db->where('ID_SEKOLAH',$id_sekolah);
		$this->db->where('DEPARTMENT',$query_dep);
		$this->db->where('ISAKTIF',1);
		$query_ta = $this->db->get()->row()->ID_TA;
		
		$this->db->select('*');
		$this->db->from('tb_master_kelas');
		$this->db->where('ID_SEKOLAH',$id_sekolah);
		$this->db->where('ID_TAHUNAJARAN',$query_ta);
		$this->db->where('ISAKTIF',1);
		if($id_kelas!='')
			$this->db->where('ID_KELAS',$id_kelas);
		$query = $this->db->get();
		return $query;
	}
	function getSmt($id_sekolah,$id_smt=''){
		$this->db->select('*');
		$this->db->from('tb_master_sekolah');
		$this->db->where('ID_SEKOLAH',$id_sekolah);
		$this->db->where('ISAKTIF',1);
		$query_dept = $this->db->get()->row()->ID_DEPARTMENT;
		
		$this->db->select('*');
		$this->db->from('tb_master_semester');
		$this->db->where('ID_SEKOLAH',$id_sekolah);
		$this->db->where('DEPARTMENT',$query_dept);
		$this->db->where('ISAKTIF',1);
		if($id_smt!='')
			$this->db->where('ID_SMT',$id_smt);
		$query = $this->db->get();
		return $query;
	}
	
	//================================================================================
	function get_grading($kode_mp, $id_sekolah, $id_ta){
		
		$this->db->select('pel.NAMA_MATA_PELAJARAN, p.*,p.KETERANGAN as KET, p.ID_SEKOLAH as ID_SEK, s.NAMA_SEKOLAH, a.TAHUN_AJARAN as NAMA_TA, p.TAHUN_AJARAN as ID_TA');
		$this->db->from('tb_pelajaran_grading p'); 
		$this->db->join('tb_master_sekolah s','s.ID_SEKOLAH=p.ID_SEKOLAH');
		$this->db->join('tb_master_tahun_ajaran a','a.ID_TA=p.TAHUN_AJARAN');
		$this->db->join('tb_pelajaran pel','pel.KODE_MATA_PELAJARAN=p.KODE_MATA_PELAJARAN and pel.TAHUN_AJARAN=p.TAHUN_AJARAN');
		// $this->db->join('tb_pelajaran_aspek_nilai an','an.ID_ASPEK=p.KODE_PENILAIAN');
		$this->db->where('p.KODE_MATA_PELAJARAN',$kode_mp);
		$this->db->where('p.ID_SEKOLAH',$id_sekolah);
		$this->db->where('p.TAHUN_AJARAN',$id_ta);
		$this->db->where('p.NILAI_MIN !=','');
		$this->db->where('p.NILAI_MAX !=','');
		$this->db->where('p.GRADE !=','');
		$this->db->order_by('p.ID_GRADE', 'asc'); 
		$this->CI->flexigrid->build_query();
		
		$return['records'] = $this->db->get();
		
		$this->db->select('pel.NAMA_MATA_PELAJARAN, p.*,p.KETERANGAN as KET, p.ID_SEKOLAH as ID_SEK, s.NAMA_SEKOLAH, a.TAHUN_AJARAN as NAMA_TA, p.TAHUN_AJARAN as ID_TA');
		$this->db->from('tb_pelajaran_grading p'); 
		$this->db->join('tb_master_sekolah s','s.ID_SEKOLAH=p.ID_SEKOLAH');
		$this->db->join('tb_master_tahun_ajaran a','a.ID_TA=p.TAHUN_AJARAN');
		$this->db->join('tb_pelajaran pel','pel.KODE_MATA_PELAJARAN=p.KODE_MATA_PELAJARAN and pel.TAHUN_AJARAN=p.TAHUN_AJARAN');
		// $this->db->join('tb_pelajaran_aspek_nilai an','an.ID_ASPEK=p.KODE_PENILAIAN');
		$this->db->where('p.KODE_MATA_PELAJARAN',$kode_mp);
		$this->db->where('p.ID_SEKOLAH',$id_sekolah);
		$this->db->where('p.TAHUN_AJARAN',$id_ta);
		$this->db->where('p.NILAI_MIN !=','');
		$this->db->where('p.NILAI_MAX !=','');
		$this->db->where('p.GRADE !=','');
		$this->db->order_by('p.ID_GRADE', 'asc'); 
		$this->CI->flexigrid->build_query(FALSE);
		
		$return['record_count'] = $this->db->count_all_results();

		return $return;
	}
	function get_gradingID($kode_mp){
		
		//Select table column
		$this->db->select('pel.NAMA_MATA_PELAJARAN, p.*,p.KETERANGAN as KET, p.ID_SEKOLAH as ID_SEK, s.NAMA_SEKOLAH, a.TAHUN_AJARAN as NAMA_TA, p.TAHUN_AJARAN as ID_TA');
		$this->db->from('tb_pelajaran_grading p');
		$this->db->join('tb_master_sekolah s','s.ID_SEKOLAH=p.ID_SEKOLAH');
		$this->db->join('tb_master_tahun_ajaran a','a.ID_TA=p.TAHUN_AJARAN');
		$this->db->join('tb_pelajaran pel','pel.KODE_MATA_PELAJARAN=p.KODE_MATA_PELAJARAN and pel.TAHUN_AJARAN=p.TAHUN_AJARAN');
		// $this->db->join('tb_pelajaran_aspek_nilai an','an.ID_ASPEK=p.KODE_PENILAIAN');
		$this->db->where('p.ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->db->where('p.TAHUN_AJARAN',$this->session->userdata('tahunajaran'));
		$this->db->where('p.KODE_MATA_PELAJARAN',$kode_mp);
		$this->db->order_by('p.ID_GRADE', 'asc'); 
		$query = $this->db->get();
		return $query;
	}
	function insert_grading($data){
		$this->db->insert('tb_pelajaran_grading',$data);
	}
	function update_grading($data, $id_grade){
		$this->db->where('ID_GRADE',$id_grade);
		$this->db->update('tb_pelajaran_grading',$data);
	}
	
	function delete_grading($kode_mp){
		$this->db->where('ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->db->where('TAHUN_AJARAN',$this->session->userdata('tahunajaran'));
		$this->db->where('KODE_MATA_PELAJARAN',$kode_mp);
		$this->db->delete('tb_pelajaran_grading'); 
	}
	
	function enableAddGrading($kode_mp){
		$this->db->select('*');
		$this->db->from('tb_pelajaran_grading');
		$this->db->where('ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->db->where('TAHUN_AJARAN',$this->session->userdata('tahunajaran'));
		$this->db->where('KODE_MATA_PELAJARAN',$kode_mp);
		$this->db->group_by('KODE_MATA_PELAJARAN');
		$query = $this->db->get();
		return $query;
	}
	
	function getAspek($id_sekolah,$kode_mp){
		$this->db->select('*');
		$this->db->from('tb_master_sekolah');
		$this->db->where('ID_SEKOLAH',$id_sekolah);
		$this->db->where('ISAKTIF',1);
		$query_dept = $this->db->get()->row()->ID_DEPARTMENT;
		
		$this->db->select('ID_TA');
		$this->db->from('tb_master_tahun_ajaran');
		$this->db->where('ID_SEKOLAH',$id_sekolah);
		$this->db->where('DEPARTMENT',$query_dept);
		$this->db->where('ISAKTIF',1);
		$query_ta = $this->db->get()->row()->ID_TA;
		
		$this->db->select('*');
		$this->db->from('tb_pelajaran_grading');
		$this->db->where('ID_SEKOLAH',$id_sekolah);
		$this->db->where('TAHUN_AJARAN',$query_ta);
		$this->db->where('KODE_MATA_PELAJARAN',$kode_mp);
		// $this->db->group_by('KODE_PENILAIAN');
		$query_aspek = $this->db->get();
		
		$data =array();
		foreach($query_aspek->result() as $row){
			$data[] = $row->KODE_PENILAIAN;
		}
		$this->db->select('*');
		$this->db->from('tb_pelajaran_aspek_nilai');
		$this->db->where('ID_SEKOLAH',$id_sekolah);
		$this->db->where('TAHUN_AJARAN',$query_ta);
		$this->db->where('KODE_MATA_PELAJARAN',$kode_mp);
		if($data != null)
			$this->db->where_not_in('ID_ASPEK', $data);
		$query = $this->db->get();
		return $query;
	}
	
	function getAspekUpdate($id_sekolah,$kode_mp){
		$this->db->select('*');
		$this->db->from('tb_master_sekolah');
		$this->db->where('ID_SEKOLAH',$id_sekolah);
		$this->db->where('ISAKTIF',1);
		$query_dept = $this->db->get()->row()->ID_DEPARTMENT;
		
		$this->db->select('ID_TA');
		$this->db->from('tb_master_tahun_ajaran');
		$this->db->where('ID_SEKOLAH',$id_sekolah);
		$this->db->where('DEPARTMENT',$query_dept);
		$this->db->where('ISAKTIF',1);
		$query_ta = $this->db->get()->row()->ID_TA;
		
		$this->db->select('*');
		$this->db->from('tb_pelajaran_aspek_nilai');
		$this->db->where('ID_SEKOLAH',$id_sekolah);
		$this->db->where('TAHUN_AJARAN',$query_ta);
		$this->db->where('KODE_MATA_PELAJARAN',$kode_mp);
		// $this->db->where_not_in('ID_ASPEK', $data);
		$query = $this->db->get();
		return $query;
	}
	
	//================================================================================
	function get_jenisujian($kode_mp, $id_sekolah, $id_ta){
		
		$this->db->select('pel.NAMA_MATA_PELAJARAN, p.*, p.ID_SEKOLAH as ID_SEK, s.NAMA_SEKOLAH, a.TAHUN_AJARAN as NAMA_TA, p.TAHUN_AJARAN as ID_TA');
		$this->db->from('tb_pelajaran_jenis_ujian p'); 
		$this->db->join('tb_master_sekolah s','s.ID_SEKOLAH=p.ID_SEKOLAH');
		$this->db->join('tb_master_tahun_ajaran a','a.ID_TA=p.TAHUN_AJARAN');
		$this->db->join('tb_pelajaran pel','pel.KODE_MATA_PELAJARAN=p.KODE_MATA_PELAJARAN and pel.TAHUN_AJARAN=p.TAHUN_AJARAN');
		$this->db->where('p.KODE_MATA_PELAJARAN',$kode_mp);
		$this->db->where('p.ID_SEKOLAH',$id_sekolah);
		$this->db->where('p.TAHUN_AJARAN',$id_ta);
		$this->db->order_by('p.ID_JENIS_UJIAN', 'asc'); 
		$this->CI->flexigrid->build_query();
		
		$return['records'] = $this->db->get();
		
		$this->db->select('pel.NAMA_MATA_PELAJARAN, p.*,p.ID_SEKOLAH as ID_SEK, s.NAMA_SEKOLAH, a.TAHUN_AJARAN as NAMA_TA, p.TAHUN_AJARAN as ID_TA');
		$this->db->from('tb_pelajaran_jenis_ujian p'); 
		$this->db->join('tb_master_sekolah s','s.ID_SEKOLAH=p.ID_SEKOLAH');
		$this->db->join('tb_master_tahun_ajaran a','a.ID_TA=p.TAHUN_AJARAN');
		$this->db->join('tb_pelajaran pel','pel.KODE_MATA_PELAJARAN=p.KODE_MATA_PELAJARAN and pel.TAHUN_AJARAN=p.TAHUN_AJARAN');
		$this->db->where('p.KODE_MATA_PELAJARAN',$kode_mp);
		$this->db->where('p.ID_SEKOLAH',$id_sekolah);
		$this->db->where('p.TAHUN_AJARAN',$id_ta);
		$this->db->order_by('p.ID_JENIS_UJIAN', 'asc'); 
		$this->CI->flexigrid->build_query(FALSE);
		
		$return['record_count'] = $this->db->count_all_results();

		return $return;
	}
	
	function get_jenisujianID($id_ujian){
		
		//Select table column
		// $this->db->select('pel.NAMA_MATA_PELAJARAN,an.ASPEK_PENILAIAN, p.*,p.KETERANGAN as KET, p.ID_SEKOLAH as ID_SEK, s.NAMA_SEKOLAH, a.TAHUN_AJARAN as NAMA_TA, p.TAHUN_AJARAN as ID_TA');
		$this->db->select('*');
		$this->db->from('tb_pelajaran_jenis_ujian p');
		$this->db->join('tb_master_sekolah s','s.ID_SEKOLAH=p.ID_SEKOLAH');
		$this->db->join('tb_master_tahun_ajaran a','a.ID_TA=p.TAHUN_AJARAN');
		$this->db->join('tb_pelajaran pel','pel.KODE_MATA_PELAJARAN=p.KODE_MATA_PELAJARAN and pel.TAHUN_AJARAN=p.TAHUN_AJARAN');
		// $this->db->where('p.ID_SEKOLAH',$id_sekolah);
		// $this->db->where('p.TAHUN_AJARAN',$id_ta);
		// $this->db->where('p.KODE_MATA_PELAJARAN',$kode_mp);
		$this->db->where('p.ID_JENIS_UJIAN',$id_ujian);
		$this->db->order_by('p.ID_JENIS_UJIAN', 'asc'); 
		$query = $this->db->get();
		return $query;
	}
	
	function validasi_jenisujian($kode_mp, $id_sekolah, $id_ta,$kode_ujian){
		
		//Select table column
		$this->db->select('count(*) as jml, ID_JENIS_UJIAN');
		$this->db->from('tb_pelajaran_jenis_ujian');
		$this->db->where('TAHUN_AJARAN',$id_ta);
		$this->db->where('ID_SEKOLAH',$id_sekolah);
		$this->db->where('KODE_MATA_PELAJARAN',$kode_mp);
		$this->db->where('KODE_UJIAN',$kode_ujian);
		$query = $this->db->get();
		return $query;
	}
	
	function insert_jenisujian($data){
		$this->db->insert('tb_pelajaran_jenis_ujian',$data);
	}
	function update_jenisujian($data, $id_ujian){
		$this->db->where('ID_JENIS_UJIAN',$id_ujian);
		$this->db->update('tb_pelajaran_jenis_ujian',$data);
	}
	
	function delete_jenisujian($id_ujian){
		$this->db->where('ID_JENIS_UJIAN',$id_ujian);
		$this->db->delete('tb_pelajaran_jenis_ujian'); 
	}
	
	//=======================================================
	function get_rpp($kode_mp,$id_sekolah,$id_ta,$id_tingkat='',$id_kelas='',$id_smt=''){
		
		$this->db->select('mp.NAMA_MATA_PELAJARAN, t.TINGKAT,t.ID_TINGKAT,k.KELAS,k.ID_KELAS,smt.SEMESTER,smt.ID_SMT, p.*, p.ID_SEKOLAH as ID_SEK, s.NAMA_SEKOLAH, a.TAHUN_AJARAN as NAMA_TA, p.TAHUN_AJARAN as ID_TA');
		$this->db->from('tb_pelajaran_rpp p'); 
		$this->db->join('tb_master_sekolah s','s.ID_SEKOLAH=p.ID_SEKOLAH');
		$this->db->join('tb_master_tahun_ajaran a','a.ID_TA=p.TAHUN_AJARAN');
		$this->db->join('tb_master_tingkat t','t.ID_TINGKAT=p.ID_TINGKAT');
		$this->db->join('tb_master_kelas k','k.ID_KELAS=p.ID_KELAS');
		$this->db->join('tb_master_semester smt','smt.ID_SMT=p.ID_SEMESTER');
		$this->db->join('tb_pelajaran mp','mp.KODE_MATA_PELAJARAN=p.KODE_MATA_PELAJARAN and mp.TAHUN_AJARAN=p.TAHUN_AJARAN');
		$this->db->where('p.TAHUN_AJARAN',$id_ta);
		$this->db->where('p.ID_SEKOLAH',$id_sekolah);
		$this->db->where('p.KODE_MATA_PELAJARAN',$kode_mp);
		// if($id_tingkat!='' and $id_kelas!='' and $id_smt!=''){
			$this->db->where('p.ID_TINGKAT',$id_tingkat);
			$this->db->where('p.ID_KELAS',$id_kelas);
			$this->db->where('p.ID_SEMESTER',$id_smt);
		// }
		$this->CI->flexigrid->build_query();
		
		$return['records'] = $this->db->get();
		
		$this->db->select('mp.NAMA_MATA_PELAJARAN, t.TINGKAT,t.ID_TINGKAT,k.KELAS,k.ID_KELAS,smt.SEMESTER,smt.ID_SMT, p.*, p.ID_SEKOLAH as ID_SEK, s.NAMA_SEKOLAH, a.TAHUN_AJARAN as NAMA_TA, p.TAHUN_AJARAN as ID_TA');
		$this->db->from('tb_pelajaran_rpp p'); 
		$this->db->join('tb_master_sekolah s','s.ID_SEKOLAH=p.ID_SEKOLAH');
		$this->db->join('tb_master_tahun_ajaran a','a.ID_TA=p.TAHUN_AJARAN');
		$this->db->join('tb_master_tingkat t','t.ID_TINGKAT=p.ID_TINGKAT');
		$this->db->join('tb_master_kelas k','k.ID_KELAS=p.ID_KELAS');
		$this->db->join('tb_master_semester smt','smt.ID_SMT=p.ID_SEMESTER');
		$this->db->join('tb_pelajaran mp','mp.KODE_MATA_PELAJARAN=p.KODE_MATA_PELAJARAN and mp.TAHUN_AJARAN=p.TAHUN_AJARAN');
		$this->db->where('p.TAHUN_AJARAN',$id_ta);
		$this->db->where('p.ID_SEKOLAH',$id_sekolah);
		$this->db->where('p.KODE_MATA_PELAJARAN',$kode_mp);
		// if($id_tingkat!='' and $id_kelas!='' and $id_smt!=''){
			$this->db->where('p.ID_TINGKAT',$id_tingkat);
			$this->db->where('p.ID_KELAS',$id_kelas);
			$this->db->where('p.ID_SEMESTER',$id_smt);
		// }
		$this->CI->flexigrid->build_query(FALSE);
		
		$return['record_count'] = $this->db->count_all_results();

		return $return;
	}
	
	function get_rppID($id_rpp){
		
		//Select table column
		$this->db->select('p.*, p.ID_SEKOLAH as ID_SEK, s.NAMA_SEKOLAH, a.TAHUN_AJARAN as NAMA_TA, p.TAHUN_AJARAN as ID_TA');
		$this->db->from('tb_pelajaran_rpp p');
		$this->db->join('tb_master_sekolah s','s.ID_SEKOLAH=p.ID_SEKOLAH');
		$this->db->join('tb_master_tahun_ajaran a','a.ID_TA=p.TAHUN_AJARAN');
		$this->db->where('p.ID_RPP',$id_rpp);
		$query = $this->db->get();
		return $query;
	}
	
	function insert_rpp($data){
		$this->db->insert('tb_pelajaran_rpp',$data);
	}
	function update_rpp($data, $id_rpp){
		$this->db->where('ID_RPP',$id_rpp);
		$this->db->update('tb_pelajaran_rpp',$data);
	}
	
	function delete_rpp($id_rpp){
		$this->db->where('ID_RPP',$id_rpp);
		$this->db->delete('tb_pelajaran_rpp'); 
	}
	function validasi_rpp($kode_mp, $id_sekolah, $thn_ajaran, $tingkat, $kelas, $smt, $kode_rpp){
		
		//Select table column
		$this->db->select('count(*) as jml, KODE_RPP');
		$this->db->from('tb_pelajaran_rpp');
		$this->db->where('TAHUN_AJARAN',$thn_ajaran);
		$this->db->where('ID_SEKOLAH',$id_sekolah);
		$this->db->where('KODE_MATA_PELAJARAN',$kode_mp);
		$this->db->where('ID_TINGKAT',$tingkat);
		$this->db->where('ID_KELAS',$kelas);
		$this->db->where('ID_SEMESTER',$smt);
		$this->db->where('KODE_RPP',$kode_rpp);
		$query = $this->db->get();
		return $query;
	}
	
	//===================================================
	
	function getPersen($id_sekolah, $id_ta, $kode_mp,$id_ujian=''){
		$this->db->select('sum(prosentase) as jml');
		$this->db->from('tb_pelajaran_jenis_ujian');
		$this->db->where('TAHUN_AJARAN',$id_ta);
		$this->db->where('ID_SEKOLAH',$id_sekolah);
		$this->db->where('KODE_MATA_PELAJARAN',$kode_mp);
		if($id_ujian != null)
			$this->db->where_not_in('ID_JENIS_UJIAN',$id_ujian);
		$query = $this->db->get();
		return $query;
	}
}

?>
